package org.example.utils;

import sun.misc.URLClassPath;
import sun.text.resources.iw.FormatData_iw_IL;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class MyClassLoader extends ClassLoader {

    private String classPath;

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        byte[] bytes = getClassData(classPath);
        Class clazz = defineClass(name, bytes, 0, bytes.length);
        return clazz;
    }

    private byte[] getClassData(String classPathName) {
        try {
            byte[] bytes = Files.readAllBytes(Paths.get(classPath));
            return bytes;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Class<?> findClass(String myPath, String className) throws Exception {
        classPath = myPath;
        return findClass(className);
    }
}